package com.example.myapp61.Util;

import android.os.Handler;
import android.os.Looper;

import com.ejlchina.okhttps.HTTP;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpUtil {
    static HttpURLConnection connection;
    static URL urls;
    static StringBuilder builder;
    static BufferedReader readLine;


    public static HTTP http = HTTP.builder()
            .callbackExecutor((Runnable run) -> {
                // 实际编码中可以吧 Handler 提出来，不需要每次执行回调都重新创建
                new Handler(Looper.getMainLooper()).post(run); // 在主线程执行
            })
            .baseUrl("https://yeqt.top:3005/ago")
            .build();


    /**
     * 网络请求工具类
     * @return
     * @throws IOException
     */
    public static StringBuilder reqStart(int page) throws IOException, JSONException {
        HttpURLConnection con = null;
        BufferedReader reader = null;
        URL url = new URL("https://yeqt.top:3005/ago/app_pagequer?page="+page);
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5000);
        con.setReadTimeout(5000);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.connect();
        InputStream in = con.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder res = new StringBuilder();
        String line;
        while ((line = reader.readLine())!=null){
            res.append(line);
        }
        con.disconnect();
        return res;
    }
    public static StringBuilder reqStart() throws IOException {
        HttpURLConnection httpURLConnection = null;
        BufferedReader reader = null;
        URL url = new URL("https://yeqt.top:3005/ago/test");
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setConnectTimeout(5000);
        httpURLConnection.setReadTimeout(5000);
        InputStream in = httpURLConnection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder res = new StringBuilder();
        String line;
        while ((line = reader.readLine())!=null){
            res.append(line);
        }
        return res;
    }

    public static StringBuilder updata(String s) throws IOException {
        urls = new URL(s);
        connection = (HttpURLConnection) urls.openConnection();
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        InputStream in = connection.getInputStream();
        readLine = new BufferedReader(new InputStreamReader(in));
        StringBuilder res = new StringBuilder();
        String line;
        while ((line = readLine.readLine())!=null){
            res.append(line);
        }
        return res;
    }

    public static HttpURLConnection getConnect(String urls) throws IOException {
        URL url = new URL(urls);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setConnectTimeout(5000);
        return httpURLConnection;
    }
}
